package com.jiayi.weiphoto.activity;

import java.util.ArrayList;

import com.jiayi.weiphoto.R;
import com.jiayi.weiphoto.entity.ImageEntity;
import com.jiayi.weiphoto.util.MyLog;
import com.jiayi.weiphoto.util.SearchSyncImageLoader;
import com.jiayi.weiphoto.util.StringUtil;
import com.jiayi.weiphoto.util.ViewsUtil;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

public class PicListAdapter extends BaseAdapter{
	
	Context context;
	View parentView;
	ArrayList<ImageEntity> urls;
	SearchSyncImageLoader imageLoader;

	public PicListAdapter(Context context, ArrayList<ImageEntity> imgUrls,
			ListView lv) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.parentView = lv;
		this.urls = imgUrls;
		imageLoader = SearchSyncImageLoader.getInstance();
		imageLoader.setCacheRootPath(StringUtil.genUserPicRootPath(context));
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return urls.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return urls.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View view, ViewGroup vg) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if(view == null){
			view = LayoutInflater.from(context).inflate(R.layout.item_listpic, null);
			holder = new ViewHolder();
			holder.imgView = (ImageView) view.findViewById(R.id.img_item_piclist);
			view.setTag(holder);
		}
		holder = (ViewHolder) view.getTag();
//		holder.imgView.setLayoutParams(new LinearLayout.LayoutParams(MainActivity.screenWidth, MainActivity.screenHeight*2/3));
		if(urls.get(position).getPic() == null){
			holder.imgView.setImageResource(R.drawable.viewholder);
//			holder.imgView.setImageBitmap(null);
			holder.imgView.setTag(urls.get(position).getUrl());
			imageLoader.loadImage(position, urls.get(position).getUrl(), new SearchSyncImageLoader.OnImageLoadListener() {
				
				@Override
				public void onImageLoad(String iconUrl, Bitmap bitmap, int pos) {
					// TODO Auto-generated method stub
					ImageView imgView = (ImageView) parentView.findViewWithTag(iconUrl);
					if(imgView != null && bitmap != null){
						MyLog.e("iconUrl funny is "+iconUrl);
						imgView.setImageBitmap(bitmap);
						imgView = ViewsUtil.prepateImageView(imgView, bitmap);
						urls.get(pos).setPic(bitmap);
					}
				}
				
				@Override
				public void onError(Integer t) {
					// TODO Auto-generated method stub
					
				}
			});
		}else{
			Bitmap bitmap = urls.get(position).getPic();
			holder.imgView.setImageBitmap(bitmap);
			ViewsUtil.prepateImageView(holder.imgView, bitmap);
		}
		return view;
	}
	
	static class ViewHolder{
		ImageView imgView;
	}

}
